﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.ClassFactory;
using Util.BasePage;
using ValidateCommon;

namespace RichECMS.ServerPages.Template
{
    public partial class TemplateManage : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                State.strWhereSql = " where intWSID="+State.intWSID +" ";
                State.intPageIndex = 1;
                this.btnAdd.HRef = this.btnAdd.HRef + "?Section=" + State.intSTID.ToString();
                InitDataBind(State.intPageIndex, AspNetPagerList.PageSize);
                AspNetPagerList.CurrentPageIndex = State.intPageIndex;
            }
        }
        /// <summary>
        /// 绑定
        /// </summary>
        private void InitDataBind(int intCurrentPage, int intPageSize)
        {
            try
            {
                using (TemplateFactory objTemplateFactory = new TemplateFactory())
                {
                    StringBuilder strWhere = new StringBuilder();

                    BusinessLogic.BusinessRules.Template objTemplate = objTemplateFactory.NewInstance();
                    int intPageFrom = intCurrentPage * intPageSize;
                    int intTotalCounts = State.strWhereSql.Trim().Length == 0 ? objTemplate.GetTotalCount() : objTemplate.GetTotalCount(State.strWhereSql.Replace("where", ""));
                    LitPageCurrent.Text = intCurrentPage.ToString();
                    LitPageCount.Text = intTotalCounts.ToString();
                    if (intTotalCounts % intPageSize != 0)
                    {
                        if ((intCurrentPage == intTotalCounts / intPageSize + 1))
                        {
                            intPageFrom = (intCurrentPage - 1) * intPageSize;
                            intPageSize = intTotalCounts % intPageSize;
                            //intPageFrom = Model.Config.JudgeDatabase(intPageFrom + intPageSize);//处理数据库差异
                            intPageFrom = intPageFrom + intPageSize;
                        }
                    }
                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " * from Template  " + State.strWhereSql + "  order by dtmInTime desc,intTemplateID desc)t order by  dtmInTime asc ,intTemplateID asc)m " + " order by   dtmInTime asc ,intTemplateID asc)mm  order by dtmInTime desc ,intTemplateID desc");
                    objTemplate = objTemplateFactory.CreatorByFullSql(strWhere.ToString());
                    if (objTemplate != null)
                    {
                        rptList.DataSource = objTemplate.Data.Template.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = intTotalCounts;
                        rptList.DataBind();
                    }
                    else
                    {
                        rptList.DataSource = objTemplateFactory.NewInstance().Data.Template.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = 0;
                        rptList.DataBind();
                        Alert("暂无数据！");
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", ""));
            }
        }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageButtonsearch(object sender, ImageClickEventArgs e)
        {
            StringBuilder sbWhere = new StringBuilder();
            sbWhere.Append(State.strWhereSql);
            if (txtTitle.Value.Trim() != "")
            {
                sbWhere.AppendFormat(" and chvName like '%{0}%'", txtTitle.Value.Trim());
            }
            if (txtDatetime.Value.Trim() != "")
            {
                sbWhere.AppendFormat(" and dtmInTime>{0}", txtDatetime.Value.Trim());
            }
            State.strWhereSql = sbWhere.ToString();
            State.intPageIndex = 1;
            InitDataBind(State.intPageIndex, AspNetPagerList.PageSize);
        }
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSort_Click(object sender, ImageClickEventArgs e)
        {

        }
        /// <summary>
        /// 删除所选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteAll_Click(object sender, ImageClickEventArgs e)
        {
            Alert(SortHidden.Value.ToString());
        }
        /// <summary>
        /// 批量放入回收站
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRecovery_Click(object sender, ImageClickEventArgs e)
        {

        }
        /// <summary>
        /// 分页方法
        /// </summary>
        private void BindToPager()
        {

        }

        /// <summary>
        /// 分页  PageChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPagerArticle_PageChanged(object sender, EventArgs e)
        {
            State.intPageIndex = AspNetPagerList.CurrentPageIndex;
            InitDataBind(AspNetPagerList.CurrentPageIndex, AspNetPagerList.PageSize);
        }
        /// <summary>
        /// rptList_ItemCommand  编辑   放入回收站  删除
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                using (TemplateFactory objFactory = new TemplateFactory())
                {
                    int intTemplateID = Convert.ToInt32(e.CommandArgument);
                    try
                    {
                        objFactory.Delete(intTemplateID);
                        BusinessLogic.BusinessRules.Template objTemplate = objFactory.Creator(intTemplateID);
                        if (objTemplate ==null)
                        {
                            AlertAndRedirect("删除成功", "TemplateManage.aspx".ToString(), false);
                        }
                        else
                        {
                            Alert("删除失败");
                        }
                    }
                    catch (Exception ex)
                    {
                        Alert(ex.ToString().Replace("\r\n", " "));
                        return;
                    }
                }
            }
            //放入回收站
            if (e.CommandName == "Recycle")
            {
            }
        }
    }
}